DogWalker FRQ Student Handout

AP Computer Science A - Student-Friendly Walkthrough

Goal: This FRQ asks you to write two methods in the DogWalker class:

Your job is to read the directions carefully and turn them into Java code.

1. Understand the Instance Variables

private int maxDogs;
private DogWalkCompany company;
      

These variables mean:

The company gives us two helpful methods:

2. Part A - walkDogs(int hour)

What is this method supposed to do?

This method should figure out how many dogs the walker can walk during one specific hour.

Important idea:
The walker can only walk the smaller of:
  • the number of dogs available
  • maxDogs

Steps to solve Part A

  1. Get the number of dogs available at that hour.
  2. Take the smaller of available dogs and maxDogs.
  3. Tell the company those dogs have been taken.
  4. Return the number of dogs walked.

Example thinking

Available Dogs maxDogs Dogs Walked
10 4 4
3 4 3

Code for Part A

public int walkDogs(int hour)
{
    int available = company.numAvailableDogs(hour);
    int dogsWalked = Math.min(available, maxDogs);
    company.updateDogs(hour, dogsWalked);
    return dogsWalked;
}
      

Why this works

3. Common Mistakes in Part A

4. Part B - dogWalkShift(int startHour, int endHour)

What is this method supposed to do?

This method calculates how much money the walker earns during an entire shift.

For each hour in the shift:

Important: The shift includes both the starting hour and the ending hour.

Steps to solve Part B

  1. Create a variable to hold the total money earned.
  2. Loop from startHour to endHour, inclusive.
  3. For each hour, call walkDogs(hour).
  4. Add dogsWalked * 5 to the total.
  5. Check if the bonus should be added.
  6. Return the total money earned.

Code for Part B

public int dogWalkShift(int startHour, int endHour)
{
    int total = 0;

    for (int hour = startHour; hour <= endHour; hour++)
    {
        int dogsWalked = walkDogs(hour);
        total += dogsWalked * 5;

        if (dogsWalked == maxDogs || (hour >= 9 && hour <= 17))
        {
            total += 3;
        }
    }

    return total;
}
      

Why this works

5. Common Mistakes in Part B

Wrong bonus example

if (dogsWalked == maxDogs)
    total += 3;
if (hour >= 9 && hour <= 17)
    total += 3;
      

This is wrong because the prompt only gives one $3 bonus per hour, not two.

6. Full Correct Solution

public int walkDogs(int hour)
{
    int available = company.numAvailableDogs(hour);
    int dogsWalked = Math.min(available, maxDogs);
    company.updateDogs(hour, dogsWalked);
    return dogsWalked;
}

public int dogWalkShift(int startHour, int endHour)
{
    int total = 0;

    for (int hour = startHour; hour <= endHour; hour++)
    {
        int dogsWalked = walkDogs(hour);
        total += dogsWalked * 5;

        if (dogsWalked == maxDogs || (hour >= 9 && hour <= 17))
        {
            total += 3;
        }
    }

    return total;
}
      

7. Easy Memory Trick

Part A: Check available, take the smaller value, update company, return result.

Part B: Loop through the hours, call Part A, add dog pay, add bonus, return total.